<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1"><title id="cc20941">CREATE SEQUENCE</title><body><p id="sql_command_desc">Defines a new sequence generator.</p><section id="section2"><title>Synopsis</title><codeblock id="sql_command_synopsis">CREATE [TEMPORARY | TEMP] SEQUENCE <varname>name</varname>
       [INCREMENT [BY] <varname>value</varname>] 
       [MINVALUE <varname>minvalue</varname> | NO MINVALUE] 
       [MAXVALUE <varname>maxvalue</varname> | NO MAXVALUE] 
       [START [ WITH ] <varname>start</varname>] 
       [CACHE <varname>cache</varname>] 
       [[NO] CYCLE] 
       [OWNED BY { <varname>table</varname>.<varname>column</varname> | NONE }]</codeblock></section><section id="section3"><title>Description</title><p><codeph>CREATE SEQUENCE</codeph> creates a new sequence number generator.
This involves creating and initializing a new special single-row table.
The generator will be owned by the user issuing the command. </p><p>If a schema name is given, then the sequence is created in the specified
schema. Otherwise it is created in the current schema. Temporary sequences
exist in a special schema, so a schema name may not be given when creating
a temporary sequence. The sequence name must be distinct from the name
of any other sequence, table, index, view, or foreign table in the same schema. </p><p>After a sequence is created, you use the <codeph>nextval()</codeph>
function to operate on the sequence. For example, to insert a row into
a table that gets the next value of a sequence:</p><codeblock>INSERT INTO distributors VALUES (nextval('myserial'), 'acme');</codeblock><p>You can also use the function <codeph>setval()</codeph> to operate on a
sequence, but only for queries that do not operate on distributed data.
For example, the following query is allowed because it resets the sequence
counter value for the sequence generator process on the master:</p><codeblock>SELECT setval('myserial', 201);</codeblock><p>But the following query will be rejected in Greenplum Database because
it operates on distributed data:</p><codeblock>INSERT INTO product VALUES (setval('myserial', 201), 'gizmo');</codeblock><p>In a regular (non-distributed) database, functions that operate on the
sequence go to the local sequence table to get values as they are needed.
 In Greenplum Database, however, keep in mind that each segment is its
own distinct database process. Therefore the segments need a single point
of truth to go for sequence values so that all segments get incremented
correctly and the sequence moves forward in the right order. A sequence
server process runs on the master and is the point-of-truth for a sequence
in a Greenplum distributed database. Segments get sequence values at
runtime from the master.</p><p>Because of this distributed sequence design, there are some limitations
on the functions that operate on a sequence in Greenplum Database:</p><ul><li id="cc148439"><codeph>lastval()</codeph> and <codeph>currval()</codeph> functions are
not supported. </li><li id="cc149500"><codeph>setval()</codeph> can only be used to set the value of the sequence
generator on the master, it cannot be used in subqueries to update records
on distributed table data.</li><li id="cc149515"><codeph>nextval()</codeph> sometimes grabs a block of values from the
master for a segment to use, depending on the query. So values may sometimes
be skipped in the sequence if all of the block turns out not to be needed
at the segment level. Note that a regular PostgreSQL database does this
too, so this is not something unique to Greenplum Database.</li></ul><p>Although you cannot update a sequence directly, you can use a query like:</p><codeblock>SELECT * FROM <varname>sequence_name</varname>;</codeblock><p>to examine the parameters and current state of a sequence. In particular,
the <varname>last_value</varname> field of the sequence shows the last value allocated
by any session.</p></section><section id="section4"><title>Parameters</title><parml><plentry><pt>TEMPORARY | TEMP</pt><pd>If specified, the sequence object is created only for this session,
and is automatically dropped on session exit. Existing permanent sequences
with the same name are not visible (in this session) while the temporary
sequence exists, unless they are referenced with schema-qualified names.
</pd></plentry><plentry><pt><varname>name</varname></pt><pd>The name (optionally schema-qualified) of the sequence to be created.
</pd></plentry><plentry><pt><varname>increment</varname></pt><pd>Specifies which value is added to the current sequence value to create
a new value. A positive value will make an ascending sequence, a negative
one a descending sequence. The default value is 1. </pd></plentry><plentry><pt><varname>minvalue</varname></pt><pt>NO MINVALUE</pt><pd>Determines the minimum value a sequence can generate. If this clause
is not supplied or <codeph>NO MINVALUE</codeph> is specified, then defaults
will be used. The defaults are 1 and -263-1 for ascending and descending
sequences, respectively. </pd></plentry><plentry><pt><varname>maxvalue</varname></pt><pt>NO MAXVALUE</pt><pd>Determines the maximum value for the sequence. If this clause is
not supplied or <codeph>NO MAXVALUE</codeph> is specified, then default
values will be used. The defaults are 263-1 and -1 for ascending and
descending sequences, respectively. </pd></plentry><plentry><pt><varname>start</varname></pt><pd>Allows the sequence to begin anywhere. The default starting value
is <varname>minvalue</varname> for ascending sequences and <varname>maxvalue</varname> for descending
ones. </pd></plentry><plentry><pt><varname>cache</varname></pt><pd>Specifies how many sequence numbers are to be preallocated and stored
in memory for faster access. The minimum (and default) value is 1 (no
cache). </pd></plentry><plentry><pt>CYCLE</pt><pt>NO CYCLE</pt><pd>Allows the sequence to wrap around when the <varname>maxvalue</varname> (for
ascending) or <varname>minvalue</varname> (for descending) has been reached. If the
limit is reached, the next number generated will be the <varname>minvalue</varname>
(for ascending) or <varname>maxvalue</varname> (for descending). If <codeph>NO CYCLE</codeph>
is specified, any calls to <codeph>nextval()</codeph> after the sequence
has reached its maximum value will return an error. If not specified,
<codeph>NO CYCLE</codeph> is the default.</pd></plentry><plentry><pt>OWNED BY <varname>table.column</varname></pt><pt>OWNED BY NONE</pt><pd>Causes the sequence to be associated with a specific table column,
such that if that column (or its whole table) is dropped, the sequence
will be automatically dropped as well. The specified table must have
the same owner and be in the same schema as the sequence. <codeph>OWNED
BY NONE</codeph>, the default, specifies that there is no such association.</pd></plentry></parml></section><section id="section5"><title>Notes</title><p>Sequences are based on bigint arithmetic, so the range cannot exceed
the range of an eight-byte integer (-9223372036854775808 to 9223372036854775807).</p><p>Although multiple sessions are guaranteed to allocate distinct sequence
values, the values may be generated out of sequence when all the sessions
are considered. For example, session A might reserve values 1..10 and
return <codeph>nextval=1</codeph>, then session B might reserve values
11..20 and return <codeph>nextval=11</codeph> before session A has generated
nextval=2. Thus, you should only assume that the <codeph>nextval()</codeph>
values are all distinct, not that they are generated purely sequentially.
Also, <varname>last_value</varname> will reflect the latest value reserved by any
session, whether or not it has yet been returned by <codeph>nextval()</codeph>.</p></section><section id="section6"><title>Examples</title><p>Create a sequence named <varname>myseq</varname>:</p><codeblock>CREATE SEQUENCE myseq START 101;</codeblock><p>Insert a row into a table that gets the next value of the sequence named <varname>idseq</varname>:</p><codeblock>INSERT INTO distributors VALUES (nextval('idseq'), 'acme'); </codeblock><p>Reset the sequence counter value on the master:</p><codeblock>SELECT setval('myseq', 201);</codeblock><p>Illegal use of <codeph>setval()</codeph> in Greenplum Database (setting
sequence values on distributed data):</p><codeblock>INSERT INTO product VALUES (setval('myseq', 201), 'gizmo'); </codeblock></section><section id="section7"><title>Compatibility</title><p><codeph>CREATE SEQUENCE</codeph> conforms to the SQL standard, with
the following exceptions: </p><ul><li id="cc148458">The <codeph>AS <varname>data_type</varname></codeph> expression specified in the SQL standard
          is not supported. </li><li id="cc148460">Obtaining the next value is done using the <codeph>nextval()</codeph>
function instead of the <codeph>NEXT VALUE FOR</codeph> expression specified
in the SQL standard. </li><li id="cc148462">The <codeph>OWNED BY</codeph> clause is a Greenplum Database extension.
</li></ul></section><section id="section8"><title>See Also</title><p><codeph><xref href="ALTER_SEQUENCE.xml#topic1" type="topic" format="dita"/></codeph>,
            <codeph><xref href="./DROP_SEQUENCE.xml#topic1" type="topic" format="dita"
        /></codeph></p></section></body></topic>
